package org.zcl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zcl.entity.po.TbUserInfo;
import org.zcl.enums.CacheKeyEnum;
import org.zcl.entity.model.CacheKey;
import org.zcl.service.TbUserInfoService;
import org.zcl.utils.CacheUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * redis控制器
 *
 * @author zhuchenglin
 * @date 2022/09/07 13:47
 */
@Api(value = "redis", tags = {"redis控制器"})
@RestController
@RequestMapping(value = "redis")
public class RedisController {

    /**
     * 结核病用户信息服务
     */
    @Resource
    private TbUserInfoService tbUserInfoService;

    /**
     * 缓存跑龙套
     */
    @Autowired
    private CacheUtil cacheUtil;

    /**
     * 得到所有
     *
     * @return {@link List}<{@link TbUserInfo}>
     */
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表", httpMethod = "GET")
    @GetMapping(value = "/getAll", produces = "application/json")
    public List<TbUserInfo> getAll() {
        List<TbUserInfo> tbUserInfos = cacheUtil.get(CacheKey.getKey(CacheKeyEnum.MAIN_DATA_CHANNELS));
        if (CollectionUtils.isEmpty(tbUserInfos)) {
            tbUserInfos = tbUserInfoService.list(new LambdaQueryWrapper<>());
            cacheUtil.set(CacheKey.getKey(CacheKeyEnum.MAIN_DATA_CHANNELS), tbUserInfos,5, TimeUnit.MINUTES);
        }
        return tbUserInfos;
    }
}
